package com.datamining.controller;

import com.datamining.service.AlgorithService;
import com.datamining.util.AlgorithmUtil;

import java.util.*;

public class AlgorithController {

	private AlgorithService algorithService = new AlgorithService();
	private AlgorithmUtil algorithmUtil = new AlgorithmUtil();



	/**
	 * 这尼玛写的是什么狗玩意
	 * 大分切
	 * @param table_name
	 */
	public String divide(String table_name , List<String> fieldNameList){
		List<String> fieldName = algorithService.findFieldName(table_name);
		String result = fieldName.get(fieldName.size() - 1);
		int propertyCount = fieldNameList.size();//可能的属性树数量

        Map<String,Double> gain = new HashMap<>();

		for (int i = 0; i < propertyCount; i++) {
            Map<String,Double> entropy = new HashMap<>();
		    int info = 0;
			String field = fieldNameList.get(i);
			List<Integer> idAllList = new ArrayList<>();
			List<String> fieldList = algorithService.findField(table_name,field);
            Map<String,List<Integer>> idMap = algorithService.findIdList(table_name,field);
			for (int j = 0; j < idMap.size(); j++) {
			    idAllList.addAll(idMap.get(fieldList.get(j)));
                List<Integer> listCount = new ArrayList<>();

                Map<String,Integer> resultMap = algorithService.findResultMap(table_name,result,idMap.get(fieldList.get(j)));
                Iterator<String> iter = resultMap.keySet().iterator();

                while (iter.hasNext()) {
                    String resultKey = iter.next();
                    listCount.add(resultMap.get(resultKey));
                }

                entropy.put(fieldList.get(j),algorithmUtil.entropy(listCount));
                listCount.clear();
			}
            List<Integer> idCount = new ArrayList<>();
            Iterator<String> iterId = idMap.keySet().iterator();
            while (iterId.hasNext()) {
                String idKey = iterId.next();
                List<Integer> idList = idMap.get(idKey);
                idCount.add(idList.size());
                info += idList.size();
            }
            Map<String,Integer> resultMap = algorithService.findResultMap(table_name,result,idAllList);
            List<Integer> resultList = new ArrayList<>();
            Iterator<String> iter = resultMap.keySet().iterator();
            while (iter.hasNext()) {
                String resultKey = iter.next();
                resultList.add(resultMap.get(resultKey));
            }
            Double allInfo = algorithmUtil.entropy(resultList);
            Iterator<String> iterResult = entropy.keySet().iterator();
            while (iterResult.hasNext()) {
                String reKey = iterResult.next();
                allInfo -= ((double)idMap.get(reKey).size()/info)*entropy.get(reKey);
            }
            gain.put(field,algorithmUtil.format(allInfo));

		}
		String maxKey = algorithmUtil.findMax(gain);

        System.out.println("哦，在这停顿");
        return maxKey;
	}


	public String div(String table_name,List<Integer> idList,List<String> fieldNameList) {
        List<String> fieldName = algorithService.findFieldName(table_name);
        String result = fieldName.get(fieldName.size() - 1);
        int propertyCount = fieldNameList.size();//所赋的属性树数量
        Map<String,List<Integer>> partResultList = algorithService.divIdList(table_name,idList);
        Map<String,Integer> resultMap = algorithService.findResultMap(table_name,result,idList);
        Map<String,Double> gain = new HashMap<>();

        for (int i = 0; i < propertyCount; i++) {
            Map<String,Double> entropy = new HashMap<>();
            int info = 0;
            String field = fieldNameList.get(i);
            List<String> fieldList = algorithService.findField(table_name,field);


        }
	    return null;
    }

}
